//
//  ZApi.m
//  ZFramework
//
//  Created by liuchengbin on 15/5/9.
//  Copyright (c) 2015年 liuchengbin. All rights reserved.
//

#import "ZApi.h"
#import "ZGlobal.h"
#import "ZHttpCache.h"
#import "Reachability.h"
@interface ZApi()
{
    ZHttpClient *http_common ;
    ZHttpClient *http_json ;
}

@end

@implementation ZApi

+ (instancetype)SharedZApi
{
    static ZApi* xb = nil;
    static dispatch_once_t once ;
    dispatch_once(&once, ^{
        xb = [[ZApi alloc] init];
    });
    return xb;
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        http_json = [[ZHttpClient alloc] init];
        http_common = [[ZHttpClient alloc] init];
    }
    return self;
}
+(BOOL) isConnectionAvailable{
    
    BOOL isExistenceNetwork = YES;
    
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    switch ([reach currentReachabilityStatus])
    {
        case NotReachable:
            isExistenceNetwork = NO;
            //NSLog(@"notReachable");
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            //NSLog(@"WIFI");
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            //NSLog(@"3G");
            break;
    }
    
    
    return isExistenceNetwork;
}

+ (void)checkNetWorkStatus{
    
    /**
     *  AFNetworkReachabilityStatusUnknown          = -1,  // 未知
     *  AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
     *  AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G
     *  AFNetworkReachabilityStatusReachableViaWiFi = 2,   // 局域网络Wifi
     */
    // 如果要检测网络状态的变化, 必须要用检测管理器的单例startMoitoring
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if(status == AFNetworkReachabilityStatusNotReachable){
            
            NSLog(@"网络连接已断开，请检查您的网络！");
            
            return ;
        }
    }];
    
}
- (void)requestWithURL:(NSString *)url
                 paras:(NSDictionary *)parasDict
                  type:(ZHttpResponseType)type
        allowFetchCache:(BOOL)allowFetchCache
               success:(void(^)(AFHTTPRequestOperation* operation, NSObject *resultObject))success
               failure:(void(^)(NSError *requestErr))failure
{
   
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    if(type == ZHttpResponseType_Common)
        [http_common requestWithURL:url paras:parasDict type:type allowFetchCache:allowFetchCache success:success failure:failure];
    else
        [http_json requestWithURL:url paras:parasDict type:type allowFetchCache:allowFetchCache success:success failure:failure];
        
}
-(void)cancel
{
    [http_common cancel];
    [http_json cancel];
}
- (void)requestWithURL2:(NSString *)url
               referer:(NSString *)refer
                 paras:(NSDictionary *)parasDict
                  type:(ZHttpResponseType)type
        allowFetchCache:(BOOL)allowFetchCache
               success:(void(^)(AFHTTPRequestOperation* operation, NSObject *resultObject))success
               failure:(void(^)(NSError *requestErr))failure
{
    if(type == ZHttpResponseType_Common)
    {
        [http_common.requestSerializer setValue:refer forHTTPHeaderField:@"Referer"];
        [http_common requestWithURL:url paras:parasDict type:type allowFetchCache:allowFetchCache success:success failure:failure];
    }
    else
    {
        [http_json.requestSerializer setValue:refer forHTTPHeaderField:@"Referer"];
        [http_json requestWithURL:url paras:parasDict type:type allowFetchCache:allowFetchCache success:success failure:failure];
    }
}
- (void)uploadImagesWithURL:(NSString *)url
                      paras:(NSDictionary *)parasDict
                     images:(NSArray*)images
                    success:(void(^)(AFHTTPRequestOperation* operation, NSObject *resultObject))success
                    failure:(void(^)(NSError *requestErr))failure
             uploadProgress:(void(^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))uploadProgress
{
    [http_common uploadImageWithURL:url paras:parasDict images:images success:success failure:failure uploadProgress:uploadProgress];
}
- (void)uploadIconWithURL:(NSString *)url
                      paras:(NSDictionary *)parasDict
                      image:(UIImage*)image
                    success:(void(^)(AFHTTPRequestOperation* operation, NSObject *resultObject))success
                    failure:(void(^)(NSError *requestErr))failure
             uploadProgress:(void(^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))uploadProgress
{
    NSMutableArray *array =[NSMutableArray array];
    if (image) {
        [array addObject:image];
    }
    [http_common iconImagesWithURL:url paras:parasDict images:array success:success failure:failure uploadProgress:uploadProgress];
}
- (void)uploadImagesWithURL:(NSString *)url
                      paras:(NSDictionary *)parasDict
                      cover:(UIImage*)cover
                     images:(NSArray*)images
                    success:(void(^)(AFHTTPRequestOperation* operation, NSObject *resultObject))success
                    failure:(void(^)(NSError *requestErr))failure
             uploadProgress:(void(^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))uploadProgress
{
    [http_common uploadImageWithURL:url paras:parasDict cover:cover images:images success:success failure:failure uploadProgress:uploadProgress];
}
@end
